﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Data.Repositories;
using mesoBoard.Data;
using System.IO;
using mesoBoard.Common.UrlHelpers;

namespace mesoBoard.Common.HtmlHelpers
{
    public static partial class mesoBoardHtmlHelpers
    {
        public static string PluginConfigValue(this HtmlHelper html, string Name)
        {
            PluginConfigRepository PluginConfigRep = new PluginConfigRepository(new mbEntities());
            return PluginConfigRep.Value(Name);
        }

        public static string SuccessMessage(this HtmlHelper html)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            string ViewDataMSG = (string)html.ViewData["Success"];
            string TempDataMSG = (string)html.ViewContext.TempData["Success"];

            html.ViewContext.TempData["Success"] = html.ViewData["Success"] = string.Empty;

            if (!string.IsNullOrEmpty(TempDataMSG))
            {
                return "<div class=\"Success\"><img src=\"" + Url.ThemeContent("~/Images/check.png") + "\" alt=\"\" />" + TempDataMSG + "</div>";
            }
            else if (!string.IsNullOrEmpty(ViewDataMSG))
            {
                return "<div class=\"Success\"><img src=\"" + Url.ThemeContent("~/Images/check.png") + "\" alt=\"\" />" + ViewDataMSG + "</div>";
            }
            else
            {
                return "";
            }
        }
        public static string ErrorMessage(this HtmlHelper html)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            string ViewDataMSG = (string)html.ViewData["Error"];
            string TempDataMSG = (string)html.ViewContext.TempData["Error"];

            html.ViewContext.TempData["Error"] = html.ViewData["Error"] = string.Empty;

            if (!string.IsNullOrEmpty(TempDataMSG))
            {
                return "<div class=\"Error\"><img src=\"" + Url.ThemeContent("~/Images/cross.png") + "\" alt=\"\" />" + TempDataMSG + "</div>";
            }
            else if (!string.IsNullOrEmpty(ViewDataMSG))
            {
                return "<div class=\"Error\"><img src=\"" + Url.ThemeContent("~/Images/cross.png") + "\" alt=\"\" />" + ViewDataMSG + "</div>";
            }
            else
            {
                return "";
            }
        }
        public static string MessageMessage(this HtmlHelper html)
        {
            UrlHelper Url = new UrlHelper(html.ViewContext.RequestContext);
            string ViewDataMSG = (string)html.ViewData["Notice"];
            string TempDataMSG = (string)html.ViewContext.TempData["Notice"];

            html.ViewContext.TempData["Notice"] = html.ViewData["Notice"] = string.Empty;

            if (!string.IsNullOrEmpty(TempDataMSG))
            {
                return "<div class=\"Notice\"><img src=\"" + Url.ThemeContent("~/Images/notice.png") + "\" alt=\"\" />" + TempDataMSG + "</div>";
            }
            else if (!string.IsNullOrEmpty(ViewDataMSG))
            {
                return "<div class=\"Notice\"><img src=\"" + Url.ThemeContent("~/Images/notice.png") + "\" alt=\"\" />" + ViewDataMSG + "</div>";
            }
            else
            {
                return "";
            }
        }

        public static string HeaderMessages(this HtmlHelper html)
        {
            ViewDataDictionary ViewData = html.ViewData;
            string errorMessage = "";
            if (ViewData["Success"] != null || html.ViewContext.TempData["Success"] != null)
            {
                errorMessage += SuccessMessage(html);
            }
            if (ViewData["Error"] != null || html.ViewContext.TempData["Error"] != null)
            {
                errorMessage += ErrorMessage(html);
            }
            if (ViewData["Notice"] != null || html.ViewContext.TempData["Notice"] != null)
            {
                errorMessage += MessageMessage(html);
            }
            return errorMessage;
        }


        public static string PageTitle(this HtmlHelper html)
        {
            ConfigRepository ConfigRep = new ConfigRepository(new mbEntities());
            if (html.ViewData["BreadCrumb"] != null && !string.IsNullOrEmpty(html.ViewData["BreadCrumb"].ToString()))
            {
                string TheCrumb = html.ViewData["BreadCrumb"].ToString();
                string[] seperators = { " -> " };
                string[] Split = TheCrumb.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                return Split.Last() + ConfigRep.Value("PageTitlePhrase");
            }
            else
            {
                return ConfigRep.Value("BoardName") + ConfigRep.Value("PageTitlePhrase");
            }
        }

        public static string BreadCrumb(this HtmlHelper html)
        {
            ConfigRepository ConfigRep = new ConfigRepository(new mbEntities());
            if (html.ViewData["BreadCrumb"] != null && !string.IsNullOrEmpty(html.ViewData["BreadCrumb"].ToString()))
            {
                string TheCrumb = html.ViewData["BreadCrumb"].ToString();
                string[] seperators = { " -> " };
                string[] Split = TheCrumb.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
                return Split.Last() + ConfigRep.Value("PageTitlePhrase");
            }
            else
            {
                return "";
            }
        }
    }
}